package com.wwm.petadopt;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Created by maaibin on 2015/5/20.
 */

public class VerticalListViewAdapter extends BaseSwipeAdapter {


    private Context mContext;
    private ArrayList<PetDetail> petDetailArray;
private View view;

    public VerticalListViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(ArrayList<PetDetail> petDetailArray) {
        this.petDetailArray = petDetailArray;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.vertical_listview_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.vertical_listview_item, null);


        SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
                YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.button_detail));
                layout.setClickToClose(true);
            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {

            }
        });
        view=v;
        v.findViewById(R.id.button_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                Bundle bundle=new Bundle();
                bundle.putString("petId", (String)view.getTag());

                Intent intent=new Intent("android.intent.action.PETDETAIL");
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        return v;
    }

    @Override
    public void fillValues(int position, View convertView) {

        //ImageLoader config
        //DisplayImageOptions displayimageOptions = new DisplayImageOptions.Builder().showStubImage(R.drawable.downloadplaceholder).cacheInMemory().cacheOnDisc().showImageOnFail(R.drawable.loading).build();

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .displayer(new RoundedBitmapDisplayer(30)) //rounded corner bitmap
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(convertView.getContext())
                // You can pass your own memory cache implementation
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .build();

        //if (ImageLoader.getInstance().isInited()) {
       //     ImageLoader.getInstance().destroy();
       // }
        ImageLoader imageLoader = ImageLoader.getInstance();
        //imageLoader.init(config);

//        imageLoadingListener = new ImageLoadingListener() {
//            @Override
//            public void onLoadingStarted(String s, View view) {
//
//            }
//
//            @Override
//            public void onLoadingFailed(String s, View view, FailReason failReason) {
//                ImageView imageView = (ImageView) view;
//                imageView.setImageResource(R.drawable.android);
//                Log.i("Failed to Load " + s, failReason.toString());
//            }
//
//            @Override
//            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//
//            }
//
//            @Override
//            public void onLoadingCancelled(String s, View view) {
//
//            }
//        };

//Imageloader usage

        ImageView img = (ImageView) convertView.findViewById(R.id.image_pet);
        //img.setScaleType(ImageView.ScaleType.FIT_CENTER);
        //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //imageLoader.displayImage("drawable://"+R.drawable.dog1, img, options);

        TextView t1 = (TextView) convertView.findViewById(R.id.txtTitle);
        TextView t2 = (TextView) convertView.findViewById(R.id.txtDesc);
        TextView t3 = (TextView) convertView.findViewById(R.id.txtDate);
        Button btn=(Button)convertView.findViewById(R.id.button_detail);

        // t.setText((position + 1) + ".");

        if (petDetailArray != null) {
            PetDetail nm = petDetailArray.get(position);
            t1.setText(nm.petName);
            t2.setText(nm.introduce);
            t3.setText(nm.adoptionAgency);
            imageLoader.displayImage(petDetailArray.get(position).imageUrl, img, options);
            btn.setTag(nm.petId);
        }
    }

    @Override
    public int getCount() {
        // return 30;
        if (petDetailArray != null) {
            return petDetailArray.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}


